package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DeleteOperation implements  IOPeration{
    public void work(BookList bookList) {
        System.out.println("删除图书");
        System.out.println("请输入你要删除的图书名称");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        int currentSize = bookList.getUsedSize();
        int index = -1;
        int i = 0;
        for (; i <currentSize ; i++) {
            Book tmp = bookList.getBook(i);  //导包
            if(tmp.getName().equals(name)){
                index = i;
                break;
            }
        }
        if(i == currentSize){
            System.out.println("没有找到的书");
            return;
        }
        //开始删除，通过后面的数组向前覆盖的方式来删除数组
        for(int j = index;j<currentSize-1;j++){  //不能取等于号，不然会越界
            Book book = bookList.getBook(j+1);
            bookList.setBooks(j,book);
        }
        bookList.setUsedSize(currentSize-1);
        //因为删除的是对象，所以把最后一个置为null
        bookList.setBooks(currentSize-1,null);
        System.out.println("删除成功");
    }
}
